<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>后台用户管理</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="/layuiAdmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiAdmin/css/admin.css" media="all">
</head>

<body>


<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel" id="del">删除</button>
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add" id="create" data-url="/admin/adminUser/create">添加</button>
                <button class="layui-btn layuiadmin-btn-useradmin" style="background: #FF5722" data-type="" id="resetPsd" data-url="/admin/adminUser/resetPsd">重置密码(123456)</button>
            </div>


            <table class="layui-table" lay-data="{url:'/admin/adminUser', page:true, limit:90, id:'test',height: 'full-25'}" lay-filter="test" id="test">
                <thead>
                <tr>
                    <th lay-data="{checkbox:true}"></th>
                    <th lay-data="{field:'id', sort: true}">ID</th>
                    <th lay-data="{field:'username',width:100, sort: true}">用户名</th>
                    <th lay-data="{field:'last_login_ip', width:100}">最后登录IP</th>
                    <th lay-data="{field:'last_login_time', width:190}">最后登录时间</th>
                    <th lay-data="{field:'remark', width:500}">备注</th>
                    <th lay-data="{field:'status', width:190, templet: '#isShow'}">状态</th>
                    <th lay-data="{width:500, templet: '#table-useradmin-webuser'}">操作</th>
                </tr>
                </thead>
            </table>
            <script type="text/html" id="isShow">
                <input type="checkbox" name="status" lay-skin="switch" lay-text="启用|禁用" lay-filter="test-table-sexDemo"
                       data-id="@{{ d.id }}" @{{ d.status == 1 ? 'checked' : '' }}>

            </script>
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs layui-bg-green" lay-event="addAuth"  href="/admin/adminUser/groupAuth/@{{d.id}}"><i class="layui-icon layui-icon-add"></i>分配权限</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"  data-url="/admin/adminUser/edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>
<script src="/layuiAdmin/layui/layui.js"></script>
<script src="/layuiAdmin/tool.js"></script>
<script>
    layui.use(['table','form'], function() {
        var table = layui.table ;
        var $ = layui.jquery,
            layer = layui.layer,
            form = layui.form;

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        form.on('switch(test-table-sexDemo)', function(obj){
            let id = $(this).data('id');
            let status = obj.elem.checked ? 1 : 0;

            $.post('/admin/adminUser/changeStatus',{id:id,status:status}, function (rJson) {
                if(!rJson.status){
                    reload();
                    layer.msg(rJson.msg, {time: 5000, icon:5});
                }
            })
        });

        var active = {
            dels: function(){
                var checkStatus = table.checkStatus('test')
                    ,data = checkStatus.data;
                var ids = [];
                for(var index in data){
                    ids.push(data[index].id);
                }

                $.post('/admin/adminUser/destroy',{id:ids}, function (rJson) {
                    if(rJson.status){
                        layer.msg(rJson.msg, {time: 5000, icon:6});
                        reload();
                    }else {
                        layer.msg(rJson.msg, {time: 5000, icon:5});
                    }
                })
            },
            resetPsd: function () {
                var checkStatus = table.checkStatus('test')
                    ,data = checkStatus.data;
                var ids = [];
                for(var index in data){
                    ids.push(data[index].id);
                }

                $.post('/admin/adminUser/resetPsd',{id:ids}, function (rJson) {
                    if(rJson.status){
                        layer.msg(rJson.msg, {time: 5000, icon:6});
                    }else {
                        layer.msg(rJson.msg, {time: 5000, icon:5});
                    }
                })
            }
        };

        $('#del').on('click',function () {
            active['dels'].call(this);
        });

        $('#resetPsd').on('click',function () {
            active['resetPsd'].call(this);
        });

        //监听工具条
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                $.post('/admin/adminUser/destroy',{id:data.id}, function (rJson) {
                    if(rJson.status){
                        layer.msg(rJson.msg, {time: 5000, icon:6});
                        obj.del();
                    }else {
                        layer.msg(rJson.msg, {time: 5000, icon:5});
                    }
                })
            } else if(obj.event === 'edit'){
                location.href='/admin/adminUser/edit/' + data.id;
            }
        });


        $('#create').click(function(){
            var url = $(this).data('url');
            $('#LAY_app_body iframe', window.parent.document).attr('src', url);
        });

        $('#search').click(function () {
            reload();
        });


        function reload() {
            table.reload('test', {
                url: '/admin/adminUser'
                ,where: {
                    name : $('#name').val(),
                }
                ,page: {
                    curr: 1
                }
            });
        }

    });
</script>
</body>

</html>